package cn.wxq.daily.empdailymgforserve.controller;

import cn.wxq.daily.empdailymgforserve.domain.SysRole;
import cn.wxq.daily.empdailymgforserve.model.Result;
import cn.wxq.daily.empdailymgforserve.model.StatusCode;
import cn.wxq.daily.empdailymgforserve.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin //跨越处理
@RestController
@RequestMapping("/system/role")
@Api(value = "SysRoleController|系统角色控制器")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 获取角色信息
     *
     * @return
     */
    @RequestMapping(value = "/findAllSysRole", method = RequestMethod.POST)
    @ApiOperation(value = "获取系统所有角色信息")
    public Result selectRoleAll() {
        List<SysRole> sysRoles = sysRoleService.selectRoleAll();
        if (sysRoles !=null){
            return new Result(true, StatusCode.OK, "获取角色成功", sysRoles);
        }else {
            return new Result(false, StatusCode.ERROR, "获取角色失败");
        }

    }

    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    @ApiOperation(value = "条件查询")
    public Result findByPage(SysRole sysRole) {
        List<SysRole> sysRoles = sysRoleService.findByPage(sysRole);
        if (sysRoles !=null){
            return new Result(true, StatusCode.OK, "获取角色成功", sysRoles);
        }else {
            return new Result(false, StatusCode.ERROR, "获取角色失败");
        }

    }
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    @ApiOperation(value = "ID查询")
    public Result selectById(Integer roleId) {
        SysRole sysRoles = sysRoleService.selectById(roleId);
        if (sysRoles !=null){
            return new Result(true, StatusCode.OK, "获取角色成功", sysRoles);
        }else {
            return new Result(false, StatusCode.ERROR, "获取角色失败");
        }

    }

    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @ApiOperation(value = "新增角色", notes = "新增角色")
    public Result insertRole(SysRole role){

        int a = sysRoleService.insertRole(role);
        if (a >= 0){
            return new Result(true,StatusCode.OK,"新增角色成功");
        }
        else {
            return new Result(false,StatusCode.ERROR,"新增失败");
        }
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新角色", notes = "更新角色")
    public Result update(SysRole role){

        int a = sysRoleService.update(role);
        if (a >= 0){
            return new Result(true,StatusCode.OK,"更新角色成功");
        }
        else {
            return new Result(false,StatusCode.ERROR,"新增失败");
        }
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    @ApiOperation(value = "删除角色", notes = "删除角色")
    public Result deleteById( Long roleId){

        int a = sysRoleService.deleteRoleById(roleId);
        if (a >= 0){
            return new Result(true,StatusCode.OK,"删除角色成功");
        }
        else {
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }

    @RequestMapping(value = "/changeStatus",method = RequestMethod.POST)
    @ApiOperation(value = "状态修改", notes = "状态修改")
    public Result changeStatus(SysRole role){

        int up = sysRoleService.updateRoleStatus(role);
        if (up >= 0){
            return new Result(true,StatusCode.OK,"状态修改成功");
        }
        else {
            return new Result(false,StatusCode.ERROR,"状态修改失败");
        }
    }
}
